import QtQuick 1.0
import com.meego 1.0
import "CommomUtil.js" as CommomUtil
import "Forum.js" as ForumUtil
import "database.js" as DataBaseUtil
import "const.js" as ConstUtil
Page {
    id: forum_main



    property bool isFavorited: false

    tools: ForumMainToolBar{
        id: toolbar_info
        isExite: forum_main.isFavorited
        onFavorited:{
            if(forum_main.isFavorited){
                favorites_dialog.open();
            }else{
                DataBaseUtil.addForumFavorite(forum_main.currentForumId,forum_main.simpleName,forum_main.forumMainImageUrl,forum_main.clubMasterName);

                forum_main.isFavorited = true
            }
        }
    }

    ConfirmDialog{
        id: favorites_dialog
        diaContent: "您将把该论坛从收藏夹中删除?"
        onAccepted: {
                DataBaseUtil.removeFavorite(forum_main.currentForumId,ConstUtil.FavoriteType.Forum);
                forum_main.isFavorited = false
        }
    }


    orientationLock : PageOrientation.LockPortrait

    property string currentClubType: ForumUtil.getCurrentClubType()

    property string currentForumId: ForumUtil.getCurrentForumId()

    property int currentIndex:1

    //需要从服务器获取的的变量
    property int forumPageNum: 1

    property string forumMainImageUrl:""

    property string forumName:""

    property string clubMasterName:""

    property string clubSeriesName:""

    property string simpleName:""


    //加载内容的url 地址
    property  string forum_url:ForumUtil.getForumUrl(currentForumId,currentClubType,currentIndex)


    //标题头
    Rectangle{
        id:title_rect
        width: 480
        height: 71
        z: 10
        anchors.top: forum_main.top;anchors.left: forum_main.left

        BorderImage {
            id: titlebar_back
            source: "../images/commom/titleBack.png"
            width: title_rect.width; height: title_rect.height
            anchors.fill: title_rect
        }

        Text {
            id: title
           // color: "#f7f0f0"
            color: "white"
            text:"第"+forum_main.currentIndex+"/"+forum_main.forumPageNum+"页"
            horizontalAlignment: Text.AlignHCenter
            font.bold: true; font.pointSize: 16;
            anchors.centerIn: titlebar_back
        }
    }
    //论坛名字区域
    Rectangle{
        id:forum_club_rect
        anchors.top: title_rect.bottom
        anchors.left: title_rect.left
        width: 480
        height: 120
        z: 10
        Image{
              id:furum_club_Image
              width: 120
              height: 90
              source: forum_main.forumMainImageUrl
              anchors.left: forum_club_rect.left; anchors.leftMargin: 20
              anchors.top: forum_club_rect.top; anchors.topMargin: 10
              fillMode: Image.PreserveAspectFit
              smooth: true
              anchors.verticalCenter:parent.verticalCenter

           }

        Rectangle{
            id:forum_club_content_rect
            width: parent.width - 150; height: parent.height
            anchors.left: furum_club_Image.right; anchors.leftMargin: 10
            anchors.top: parent.top


            Column {
                   id: column

                   Text {
                        id: forum_name
                        width: forum_club_content_rect.width;
                        anchors.left: forum_club_content_rect.left; anchors.top: forum_club_content_rect.top
                        anchors.topMargin:10
                        text: forum_main.forumName
                        wrapMode: Text.WordWrap
                        font { bold: true; family: "Helvetica"; pointSize: 18 }

                    }

                    Text {
                        id:clubMaster_name
                        width:forum_name.width;
                        anchors.left: forum_name.left; anchors.top: forum_name.bottom
                        anchors.topMargin: 5
                        text: "版主："+forum_main.clubMasterName
                        wrapMode: Text.WordWrap
                        color: "blue"
                        font { bold: false; family: "Helvetica"; pointSize: 12 }
                         horizontalAlignment: Text.AlignLeft
                    }

                    Text {
                        id:clubSeries_name
                        width: clubMaster_name.width; height: 30
                        anchors.left: clubMaster_name.left; anchors.top: clubMaster_name.bottom
                        anchors.bottomMargin: 10
                        text: "车系："+forum_main.clubSeriesName
                        wrapMode: Text.WordWrap
                        color:"blue"
                        font { bold: false; family: "Helvetica"; pointSize: 12 }
                         horizontalAlignment: Text.AlignLeft
                    }

              }
        }

    }
    //论坛帖子区域
    Rectangle{
        id:forum_clubTopic_list_rect
        width:forum_main.width
        height: forum_main.height-231
        anchors.top: forum_club_rect.bottom
        anchors.topMargin: 5

        Row{
            //展示的内容

                 FlickListView {
                            id: forum_club_list
                            width: forum_clubTopic_list_rect.width
                            height: forum_clubTopic_list_rect.height
                            model: forumClubListModel
                            delegate: ForumClubListDelegate{}
                            footer:list_footer

                  }

           }
     }

    Component{
        id:list_footer
        Item{
            id:listview_footer
            width:480
            height: 60

            Rectangle{
                id:center_Rect
                width:100
                height:parent.height
                anchors.centerIn: parent
            }

            Rectangle{
                id:pre_page_rect
                width:90
                height: parent.height
                anchors.right:center_Rect.left

                color:"#ebeff9"
                border.width: 1
                border.color: "black"
                radius: 10
                Text{
                    id:pre_text
                    text:"上一页"
                    font { family: "Helvetica";  bold: true ;pointSize:10}
                    anchors {
                        horizontalCenter: pre_page_rect.horizontalCenter
                        verticalCenter: pre_page_rect.verticalCenter

                    }
                }
                MouseArea{
                    id:pre_mouse
                    anchors.fill: pre_page_rect
                    onClicked: {
                        //pre btn click
                        console.log("pre btn");
                        if(forum_main.currentIndex>1){
                            forumClubListModel.loadClubList(forum_main.currentForumId,forum_main.currentClubType,forum_main.currentIndex-1);
                        }
                    }
                }

             }

             Rectangle{
                    id:next_rect
                     width:240
                     color:"#ebeff9"
                     border.width: 1
                     border.color: "black"
                     radius: 10
                     anchors.left: pre_page_rect.right
                     Text{
                         id:next_text
                         text:"下一页"
                         font { family: "Helvetica";  bold: true ;pointSize:10}
                         anchors {
                             horizontalCenter: next_rect.horizontalCenter
                             verticalCenter: next_rect.verticalCenter
                         }
                     }
                     MouseArea{
                         id:next_mouse
                         onClicked: {
                             console.log("next btn click");
                             if(forum_main.currentIndex<forum_main.forumPageNum){
                                 forumClubListModel.loadClubList(forum_main.currentForumId,forum_main.currentClubType,forum_main.currentIndex+1);
                             }
                         }
                     }

            }
        }
    }

    ForumMainListModel{
        id:forumClubListModel

    }
    ListModel{
        id: search_model
    }
    //创建结束，加载数据
    Component.onCompleted: {
        forum_main.isFavorited = DataBaseUtil.isFavoriteExists(forum_main.currentForumId,ConstUtil.FavoriteType.Forum);

       forumClubListModel.loadClubList(forum_main.currentForumId,forum_main.currentClubType,forum_main.currentIndex);
    }
    //改变帖子类型
    function changeModelList(type){
        if(type=='1'){//精华帖
           search_model.clear();

            var textValue = "精"
            for(var i = 0; i < forumClubListModel.count; i++){
                var letter = forumClubListModel.get(i).topicTypeName;
                if(letter === textValue){
                    search_model.append(forumClubListModel.get(i))
                }
            }
            forum_club_list.model=search_model

        }else{
            forum_club_list.model=forumClubListModel
        }
    }


}
